package blue.starter.internal.mqtt.property;

import blue.starter.internal.core.util.AssertUtil;
import blue.starter.internal.core.util.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;

/**
 * @author Jin Zheng
 * @since 1.0 2020-12-28
 */
public class MqttConfigProperties implements InitializingBean {
    private static Logger logger = LoggerFactory.getLogger(MqttConfigProperties.class);
    public static final String RANDOM = "$RANDOM";

    private String name;
    private String producerName;
    private String consumerName;
    private String broker;
    private String username;
    private String password;
    private String clientId;
    private int count = 1;
    private int keepAlive;
    private int reconnectDelay;
    private String producerListener;
    private int defaultQos;
    private String defaultTaskExecutor;
    private String defaultExceptionHandler;
    private List<MqttListenerProperties> listeners;

    public MqttConfigProperties() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProducerName()
    {
        return producerName;
    }

    public void setProducerName(String producerName)
    {
        this.producerName = producerName;
    }

    public String getConsumerName()
    {
        return consumerName;
    }

    public void setConsumerName(String consumerName)
    {
        this.consumerName = consumerName;
    }

    public String getBroker() {
        return broker;
    }

    public void setBroker(String broker) {
        this.broker = broker;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public int getCount()
    {
        return count;
    }

    public void setCount(int count)
    {
        this.count = count;
    }

    public int getKeepAlive()
    {
        return keepAlive;
    }

    public void setKeepAlive(int keepAlive)
    {
        this.keepAlive = keepAlive;
    }

    public int getReconnectDelay()
    {
        return reconnectDelay;
    }

    public void setReconnectDelay(int reconnectDelay)
    {
        this.reconnectDelay = reconnectDelay;
    }

    public int getDefaultQos() {
        return defaultQos;
    }

    public void setDefaultQos(int defaultQos) {
        this.defaultQos = defaultQos;
    }

    public String getProducerListener() {
        return producerListener;
    }

    public void setProducerListener(String producerListener) {
        this.producerListener = producerListener;
    }

    public String getDefaultTaskExecutor()
    {
        return defaultTaskExecutor;
    }

    public void setDefaultTaskExecutor(String defaultTaskExecutor)
    {
        this.defaultTaskExecutor = defaultTaskExecutor;
    }

    public String getDefaultExceptionHandler()
    {
        return defaultExceptionHandler;
    }

    public void setDefaultExceptionHandler(String defaultExceptionHandler)
    {
        this.defaultExceptionHandler = defaultExceptionHandler;
    }

    public List<MqttListenerProperties> getListeners() {
        return listeners;
    }

    public void setListeners(List<MqttListenerProperties> listeners) {
        this.listeners = listeners;
    }

    @Override
    public void afterPropertiesSet() {
        AssertUtil.notEmpty(name, "name");
        AssertUtil.notEmpty(broker, "broker");
        AssertUtil.notEmpty(username, "username");
        AssertUtil.notEmpty(password, "password");
        AssertUtil.notEmpty(clientId, "clientId");

        if (clientId.contains(MqttConfigProperties.RANDOM)) {
            String random = RandomUtil.rand(RandomUtil.RandomType.UPPER_LOWER_CASE_NUMBER, 10);
            clientId = clientId.replace(MqttConfigProperties.RANDOM, random);
        }
    }
}
